package com.project.controller;

import com.project.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TestController {

    @RequestMapping("/")
    public String getIndex(HttpServletRequest req){
        //在请求对象中，绑定共享数据
        req.setAttribute("name","张三");
        req.setAttribute("age",20);

        req.setAttribute("user",new UserBean(1,"胡勇",LocalDate.parse("1998-03-02")));
        List<UserBean> list = new ArrayList<>();
        list.add(new UserBean(1,"胡克",LocalDate.parse("2000-09-10")));
        list.add(new UserBean(5,"李可",LocalDate.parse("1998-03-30")));
        list.add(new UserBean(12,"王中江",LocalDate.parse("2001-11-15")));
        list.add(new UserBean(8,"刘一山",LocalDate.parse("2002-02-06")));

        req.setAttribute("userList",list);

        req.setAttribute("totalPage",5);
        //请求转发到指定页面
        return "/index.html";
    }

    @RequestMapping("test")
    public String test(Model model){
        model.addAttribute("age",30);
        return "/index.html";
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public UserBean getUser(){
        return new UserBean(1,"张三", LocalDate.parse("2001-02-03"));
    }
}
